namespace Drill.Core.Utilities
{
	/// <summary>
	/// Converts string representations of 32-bit integers to an <see cref="int"/>.
	/// </summary>
    public static class StringToInt32Converter
    {
		/// <summary>
		/// Converts the specified string value.
		/// </summary>
		/// <param name="value">The string representation of a 32-bit integer value.</param>
		/// <param name="defaultValue">
		/// The default value to be returned if the <paramref name="value"/> cannot be parsed.
		/// </param>
		/// <returns>
		/// An <see cref="int"/> value that was either converted from the specified 
		/// <paramref name="value"/> of the specified <paramref name="defaultValue"/>.
		/// </returns>
        public static int Convert(string value, int defaultValue)
        {
            // If the value is null or empty, 
            // we cannot parse the boolean value.
            // In this case, return the defaultValue.
            if (string.IsNullOrEmpty(value))
            {
                return defaultValue;
            }

            // Attempt to parse the value using the System.Int32 class.
            // If th parsing fails, return the defaultValue.

            int result;

            return int.TryParse(value, out result) ? result : defaultValue;
        }
    }
}